package cn.yangtianxia.system.service.impl;

import cn.yangtianxia.basic.service.Impl.BaseServiceImpl;
import cn.yangtianxia.system.domain.Menu;
import cn.yangtianxia.system.mapper.MenuMapper;
import cn.yangtianxia.system.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangtianxia
 * @since 2022-04-06
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> queryTree() {
        List<Menu> allMenu = menuMapper.loadAll();
        HashMap<Long, Menu> allMap = new HashMap<>();
        for (Menu menu : allMenu){
            allMap.put(menu.getId(),menu);
        }
        List<Menu> menus = new ArrayList<>();
        for (Menu tmp : allMenu){
            if (tmp.getParentId() == null){
                menus.add(tmp);
            } else {
                Menu parent = allMap.get(tmp.getParentId());
                if (parent != null){
                    parent.getChildren().add(tmp);
                }
            }
        }
        return menus;
    }
}
